home *** CD-ROM | disk | FTP | other *** search
/ Mundo do CD-ROM 118 / cdrom118.iso / jogos / mitigo9 / Mitigo9Demo Setup.exe / {app} / main.cs < prev    next >
Encoding:
Text File  |  2006-03-25  |  12.0 KB  |  422 lines

  1. //-----------------------------------------------------------------------------
  2. // Torque 2D. 
  3. // Copyright (C) GarageGames.com, Inc.
  4. //-----------------------------------------------------------------------------
  5.  
  6.  
  7.  
  8. // --------------------------------------------------------------------
  9. // Bit-Shifter Helper.
  10. // --------------------------------------------------------------------
  11. function BIT(%bitIndex)
  12. {
  13.     return 1<<%bitIndex;
  14. }
  15.  
  16.  
  17. //-----------------------------------------------------------------------------
  18. // Add Mod-Token to front of mods list.
  19. //-----------------------------------------------------------------------------
  20. function pushFront(%list, %token, %delim)
  21. {
  22.     // Is the list empty?
  23.     if (%list !$= "")
  24.         // No, so add new token.
  25.         return %token @ %delim @ %list;
  26.         
  27.     // Yes, so just use new token.
  28.     return %token;
  29. }
  30.  
  31. //-----------------------------------------------------------------------------
  32. // Add Mod-Token to back of mods list.
  33. //-----------------------------------------------------------------------------
  34. function pushBack(%list, %token, %delim)
  35. {
  36.     // Is the list empty?
  37.     if (%list !$= "")
  38.         // No, so add new token.
  39.         return %list @ %delim @ %token;
  40.         
  41.     // Yes, so just use new token.
  42.     return %token;
  43. }
  44.  
  45.  
  46. //-----------------------------------------------------------------------------
  47. // Remove Mod-Token from the mods list.
  48. //-----------------------------------------------------------------------------
  49. function popFront(%list, %delim)
  50. {
  51.     // Next Token.
  52.     return nextToken(%list, unused, %delim);
  53. }
  54.  
  55.  
  56. //-----------------------------------------------------------------------------
  57. // The displayHelp, onStart, onExit and parseArgs function are overriden
  58. // by mod packages to get hooked into initialization and cleanup. 
  59. //-----------------------------------------------------------------------------
  60. function onStart()
  61. {
  62.    // Default startup function
  63. }
  64.  
  65.  
  66. //-----------------------------------------------------------------------------
  67. // Exit Engine.
  68. //-----------------------------------------------------------------------------
  69. function onExit()
  70. {
  71.    // OnExit is called directly from C++ code, whereas onStart is
  72.    // invoked at the end of this file.
  73. }
  74.  
  75.  
  76. //-----------------------------------------------------------------------------
  77. // Parse Arguments.
  78. //-----------------------------------------------------------------------------
  79. function parseArgs()
  80. {
  81.    // Here for mod override, the arguments have already
  82.    // been parsed.
  83. }   
  84.  
  85.  
  86. //-----------------------------------------------------------------------------
  87. // Help Package.
  88. //-----------------------------------------------------------------------------
  89. package Help
  90. {
  91.    function onExit()
  92.    {
  93.       // Override onExit when displaying help
  94.    }
  95. };
  96.  
  97.  
  98. //-----------------------------------------------------------------------------
  99. // Default Help Display.
  100. //-----------------------------------------------------------------------------
  101. function displayHelp()
  102. {
  103.     // Activate the Help Package.
  104.     activatePackage(Help);
  105.  
  106.     // Notes on logmode: console logging is written to console.log.
  107.     // -log 0 disables console logging.
  108.     // -log 1 appends to existing logfile; it also closes the file
  109.     // (flushing the write buffer) after every write.
  110.     // -log 2 overwrites any existing logfile; it also only closes
  111.     // the logfile when the application shuts down.  (default)
  112.     
  113.     error(
  114.       "T2D command line options:\n"@
  115.       "  -log <logmode>         Logging behavior; see main.cs comments for details\n"@
  116.       "  -game <game_name>      Reset list of mods to only contain <game_name>\n"@
  117.       "  <game_name>            Works like the -game argument\n"@
  118.       "  -mod <mod_name>        Add <mod_name> to list of mods\n"@
  119.       "  -console               Open a separate console\n"@
  120.       "  -jSave  <file_name>    Record a journal\n"@
  121.       "  -jPlay  <file_name>    Play back a journal\n"@
  122.       "  -jDebug <file_name>    Play back a journal and issue an int3 at the end\n"@
  123.       "  -help                  Display this help message\n"
  124.     );
  125. }
  126.  
  127.  
  128. //--------------------------------------------------------------------------
  129. // Execute MOD Directory.
  130. //--------------------------------------------------------------------------
  131. function loadDir( %dir )
  132. {
  133.     // Set Mod Paths.
  134.     setModPaths( pushback($userMods, %dir, ";") );
  135.     
  136.     // Execute Boot-strap file.
  137.     exec( %dir @ "/main.cs" );
  138. }
  139.  
  140.  
  141. //--------------------------------------------------------------------------
  142. // Execute startup scripts for each mod, starting at base and working up.
  143. //--------------------------------------------------------------------------
  144. function loadMods( %modPath )
  145. {
  146.     // Get MOD Path.
  147.     %modPath = nextToken(%modPath, token, ";");
  148.     
  149.     // Did we get a MOD token?
  150.     if (%modPath !$= "")
  151.         // Yes, so recursively load it.
  152.         loadMods(%modPath);
  153.  
  154.     // Execute MOD Boot-strap file.
  155.     if( exec(%token @ "/main.cs") != true )
  156.     {
  157.         // Problem.
  158.         error("Error: Unable to find specified mod: " @ %token );
  159.         // Decrease MOD Count.
  160.         $modcount--;
  161.     }
  162. }
  163.  
  164.  
  165.  
  166. //-----------------------------------------------------------------------------
  167. //
  168. // Before Release, set the "runWithEditors" option below to false and
  169. // simply remove the appropriate editor directories from T2D.
  170. //
  171. //-----------------------------------------------------------------------------
  172. $runWithEditors = false;
  173. $modcount = 1;
  174. $defaultGame = "Mitigo";
  175. $userMods = $defaultGame;
  176. $displayHelp = false;
  177.  
  178. // Add Editors?
  179. if ( $runWithEditors )
  180. {
  181.     // Yes!
  182.     $userMods = "particleeditor;tileeditor;" @ $userMods;
  183.     $modcount += 2;
  184. }
  185.  
  186. //------------------------------------------------------------------------------
  187. // Process command line arguments
  188. //------------------------------------------------------------------------------
  189. for ($i = 1; $i < $Game::argc ; $i++)
  190. {
  191.     // Set Current Argument.
  192.     $arg = $Game::argv[$i];
  193.     // Set Next Argument.
  194.     $nextArg = $Game::argv[$i+1];
  195.     // Check if we've got another argument.
  196.     $hasNextArg = $Game::argc - $i > 1;
  197.     // Log-Mode Off by default.
  198.     $logModeSpecified = false;
  199.     
  200.     // Handle Argument Appropriately.
  201.     switch$ ($arg)
  202.     {        
  203.         // ****************************************************
  204.         // Console Logging.
  205.         // ****************************************************
  206.         case "-log":
  207.          $argUsed[$i]++;
  208.          if ($hasNextArg)
  209.          {
  210.             // Turn on console logging
  211.             if ($nextArg != 0)
  212.             {
  213.                // Dump existing console to logfile first.
  214.                $nextArg += 4;
  215.             }
  216.             setLogMode($nextArg);
  217.             $logModeSpecified = true;
  218.             $argUsed[$i+1]++;
  219.             $i++;
  220.          }
  221.          else
  222.             error("Error: Missing Command Line argument. Usage: -log <Mode: 0,1,2>");
  223.         
  224.         
  225.         // ****************************************************
  226.         // MODs.
  227.         // ****************************************************
  228.         case "-mod":
  229.          $argUsed[$i]++;
  230.          if ($hasNextArg)
  231.          {
  232.             // Append the mod to the end of the current list
  233.             $userMods = strreplace($userMods, $nextArg, "");
  234.             $userMods = pushFront($userMods, $nextArg, ";");
  235.             $argUsed[$i+1]++;
  236.             $i++;
  237.             $modcount++;
  238.          }
  239.          else
  240.             error("Error: Missing Command Line argument. Usage: -mod <mod_name>");
  241.  
  242.             
  243.         // ****************************************************
  244.         // GAME (Base or Complete Mod replacement).
  245.         // ****************************************************
  246.         case "-game":
  247.          $argUsed[$i]++;
  248.          if ($hasNextArg)
  249.          {
  250.             // Remove all mods, start over with game
  251.             $userMods = $nextArg;
  252.             $argUsed[$i+1]++;
  253.             $i++;
  254.         $modcount = 1;
  255.          }
  256.          else
  257.             error("Error: Missing Command Line argument. Usage: -game <game_name>");
  258.         
  259.         
  260.         // ****************************************************
  261.         // Console Mode. 
  262.         // ****************************************************
  263.         case "-console":
  264.          enableWinConsole(true);
  265.          $argUsed[$i]++;
  266.  
  267.          
  268.         // ****************************************************
  269.         // Journal Save.
  270.         // ****************************************************
  271.         case "-jSave":
  272.          $argUsed[$i]++;
  273.          if ($hasNextArg)
  274.          {
  275.             echo("Saving event log to journal: " @ $nextArg);
  276.             saveJournal($nextArg);
  277.             $argUsed[$i+1]++;
  278.             $i++;
  279.          }
  280.          else
  281.             error("Error: Missing Command Line argument. Usage: -jSave <journal_name>");
  282.  
  283.             
  284.         // ****************************************************
  285.         // Journal Play.
  286.         // ****************************************************
  287.         case "-jPlay":
  288.          $argUsed[$i]++;
  289.          if ($hasNextArg)
  290.          {
  291.             playJournal($nextArg,false);
  292.             $argUsed[$i+1]++;
  293.             $i++;
  294.          }
  295.          else
  296.             error("Error: Missing Command Line argument. Usage: -jPlay <journal_name>");
  297.  
  298.             
  299.         // ****************************************************
  300.         // Debugging.
  301.         // ****************************************************
  302.         case "-jDebug":
  303.          $argUsed[$i]++;
  304.          if ($hasNextArg)
  305.          {
  306.             playJournal($nextArg,true);
  307.             $argUsed[$i+1]++;
  308.             $i++;
  309.          }
  310.          else
  311.             error("Error: Missing Command Line argument. Usage: -jDebug <journal_name>");
  312.         
  313.         
  314.         // ****************************************************
  315.         // Command-Line Help.
  316.         // ****************************************************
  317.         case "-help":
  318.          $displayHelp = true;
  319.          $argUsed[$i]++;
  320.         
  321.         
  322.         // ****************************************************
  323.         // Default (Ignore).
  324.         // ****************************************************
  325.         default:
  326.          $argUsed[$i]++;
  327.          if($userMods $= "")
  328.             $userMods = $arg;
  329.     }
  330. }
  331.  
  332.  
  333. //--------------------------------------------------------------------------
  334. // Do we have any mods?
  335. //--------------------------------------------------------------------------
  336. if( $modcount == 0 )
  337. {
  338.     // No, so reset user-mods.
  339.     $userMods = $defaultGame;
  340.     $modcount = 1;
  341. }
  342.  
  343. //--------------------------------------------------------------------------
  344. // Default to a new logfile each session.
  345. //--------------------------------------------------------------------------
  346. if ( !$logModeSpecified )
  347. {
  348. //   setLogMode(6);
  349.    setLogMode(0);
  350. }
  351.  
  352. //--------------------------------------------------------------------------
  353. // Set the mod path which dictates which directories will be visible
  354. // to the scripts and the resource engine.
  355. //--------------------------------------------------------------------------
  356. setModPaths( $userMods );
  357.  
  358. //--------------------------------------------------------------------------
  359. // Get the first mod on the list, which will be the last to be applied... this
  360. // does not modify the list.
  361. //--------------------------------------------------------------------------
  362. nextToken( $userMods, currentMod, ";" );
  363.  
  364. //--------------------------------------------------------------------------
  365. // Start the MOD loading process...
  366. //--------------------------------------------------------------------------
  367. echo( "--------- Loading MODS ---------" );
  368. loadMods( $userMods );
  369. echo( "" );
  370.  
  371. // Are there any mods?
  372. if( $modcount == 0 )
  373. {
  374.     // No, so enable console.
  375.     enableWinConsole(true);
  376.     // Show Error.
  377.     error("Error: Unable to load any specified mods");
  378.     // Quit!
  379.     quit();    
  380. }
  381.  
  382. //--------------------------------------------------------------------------
  383. // Parse the command line arguments.
  384. //--------------------------------------------------------------------------
  385. echo("--------- Parsing Arguments ---------");
  386. parseArgs();
  387.  
  388.  
  389. //--------------------------------------------------------------------------
  390. // Either display the help message or startup the application.
  391. //--------------------------------------------------------------------------
  392.  
  393. // Are we displaying help?
  394. if ( $displayHelp )
  395. {
  396.     // Yes, so enable console.
  397.     enableWinConsole(true);
  398.     // Display Help.
  399.     displayHelp();
  400.     // Quit!
  401.     quit();
  402. }
  403. else
  404. {
  405.     // Start-up the script mods.
  406.     onStart();
  407.     // User Info.
  408.     echo("T2D Engine initialized...");
  409. }
  410.  
  411. //--------------------------------------------------------------------------
  412. // Display an error message for unused arguments
  413. //--------------------------------------------------------------------------
  414. for ($i = 1; $i < $Game::argc; $i++)
  415. {
  416.     // Unused?
  417.     if ( !$argUsed[$i] )
  418.         // Yes, so show error.
  419.         error("Error: Unknown command line argument: " @ $Game::argv[$i]);
  420. }
  421.  
  422.